package com.myfoon.model.entity;


import java.io.Serializable;

import java.sql.Timestamp;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.QueryHint;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Entity
@Table(name = "EVENT")
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
public class Event implements Serializable, Comparable {
    private Long id;
    private User owner;

    private Timestamp date;
    private String message;

    private Timestamp created;

    private List<EventPhoto> photos = new ArrayList<EventPhoto>();
    private List<EventUser> users = new ArrayList<EventUser>();
    private List<EventVideo> videos = new ArrayList<EventVideo>();

    public Event() {
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Id
    @GeneratedValue(generator = "EventSequence",
                    strategy = GenerationType.AUTO)
    @SequenceGenerator(name = "EventSequence", allocationSize = 1,
                       sequenceName = "EVENT_SEQ_01")
    @Column(name = "ID")
    public Long getId() {
        return id;
    }

    public void setOwner(User owner) {
        this.owner = owner;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "OWNER")
    public User getOwner() {
        return owner;
    }

    public void setDate(Timestamp date) {
        this.date = date;
    }

    @Column(name = "_DATE")
    public Timestamp getDate() {
        return date;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Column(name = "MESSAGE")
    public String getMessage() {
        return message;
    }


    public void setCreated(Timestamp created) {
        this.created = created;
    }

    @Column(name = "CREATED")
    public Timestamp getCreated() {
        return created;
    }

    public void setPhotos(List<EventPhoto> photos) {
        this.photos = photos;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY,
               mappedBy = "event")
    public List<EventPhoto> getPhotos() {
        return photos;
    }

    public void setUsers(List<EventUser> users) {
        this.users = users;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY,
               mappedBy = "event")
    public List<EventUser> getUsers() {
        return users;
    }

    public void setVideos(List<EventVideo> videos) {
        this.videos = videos;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY,
               mappedBy = "event")
    public List<EventVideo> getVideos() {
        return videos;
    }

    public int compareTo(Object o) {
        return -1 * (this.getCreated().compareTo(((Event)o).getCreated()));
    }
}
